#include <stdio.h>
#include <stdlib.h>

#define MAX_VERTICES 50  // 最大顶点数

typedef struct {
    int num_vertices;             // 图中的顶点数量
    int adjacency_matrix[MAX_VERTICES][MAX_VERTICES]; // 邻接矩阵
} UndirectedGraph;

// 初始化图
void initialize_graph(UndirectedGraph *graph, int num_vertices) {
    graph->num_vertices = num_vertices;
    for (int i = 0; i < num_vertices; i++) {
        for (int j = 0; j < num_vertices; j++) {
            graph->adjacency_matrix[i][j] = 0;
        }
    }
}

// 添加边
void add_edge(UndirectedGraph *graph, int u, int v) {
    if (u >= 0 && u < graph->num_vertices && v >= 0 && v < graph->num_vertices) {
        graph->adjacency_matrix[u][v] = 1; // 标记为1表示u和v之间有边
        graph->adjacency_matrix[v][u] = 1; // 无向图是对称的
    }
}

// 删除边
void remove_edge(UndirectedGraph *graph, int u, int v) {
    if (u >= 0 && u < graph->num_vertices && v >= 0 && v < graph->num_vertices) {
        graph->adjacency_matrix[u][v] = 0; // 将边(u, v)的值设置为0
        graph->adjacency_matrix[v][u] = 0; // 对称操作
    }
}

// 打印邻接矩阵
void print_graph(UndirectedGraph *graph) {
    for (int i = 0; i < graph->num_vertices; i++) {
        for (int j = 0; j < graph->num_vertices; j++) {
            printf("%d ", graph->adjacency_matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    UndirectedGraph graph;
    initialize_graph(&graph, 5); // 初始化一个有5个顶点的图
    add_edge(&graph, 0, 1);
    add_edge(&graph, 0, 4);
    add_edge(&graph, 1, 2);
    add_edge(&graph, 1, 3);
    add_edge(&graph, 1, 4);
    add_edge(&graph, 2, 3);
    add_edge(&graph, 3, 4);

    printf("邻接矩阵:\n");
    print_graph(&graph);

    return 0;
}
